package pers.qianyu.converter;


import cn.hutool.core.util.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.*;
import org.springframework.stereotype.*;
import pers.qianyu.domain.common.*;
import pers.qianyu.domain.dto.*;
import pers.qianyu.domain.entity.*;
import pers.qianyu.domain.vo.*;

import java.util.*;
import java.util.stream.*;

/**
 * @author mizzle rain
 * @date 2020-11-05 16:00
 */
@Component
public class UserConverter {
    public UserDO convertToDO(UserDTO userDTO) {
        UserDO userDO = new UserDO();
        userDO.setUsername(userDTO.getUsername());
        userDO.setPassword(userDTO.getPassword());
        userDO.setEmail(userDTO.getEmail());
        userDO.setPhone(userDTO.getPhone());
        userDO.setAge(userDTO.getAge());
        userDO.setCreateTime(userDTO.getCreateTime());
        userDO.setVersion(userDTO.getVersion());
        return userDO;
    }

    public UserVO convertToVO(UserDO userDO) {
        UserVO userVO = new UserVO();
        userVO.setUsername(userDO.getUsername());
        userVO.setEmail(userDO.getEmail());
        userVO.setAge(userDO.getAge());
        userVO.setPassword("******");
        String phone = userDO.getPhone();
        if (StrUtil.isNotBlank(phone)) {
            userVO.setPhone(phone.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2"));
        }
        return userVO;
    }

    public Pagination<UserVO> convertToPagination(Page<UserDO> userDOPage) {
        Pagination<UserVO> pagination = new Pagination<>();
        pagination.setCurrent(userDOPage.getCurrent());
        pagination.setTotal(userDOPage.getTotal());
        pagination.setSize(userDOPage.getSize());
        pagination.setRecords(
                Optional.ofNullable(userDOPage.getRecords())
                        .map(List::stream)
                        .orElseGet(Stream::empty)
                        .map(this::convertToVO)
                        .collect(Collectors.toList()));
        return pagination;
    }

    public UserDO convertToDO(UserQueryDTO userQueryDTO) {
        UserDO userDO = new UserDO();
        userDO.setUsername(userQueryDTO.getUsername());
        return userDO;
    }
}
